package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.dto.CommunityFloorsDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CommunityFloors;
import com.ruoyi.system.service.ICommunityFloorsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * floorsController
 *
 * @author yhb
 * @date 2024-03-26
 */
@RestController
@RequestMapping("/system/floors")
public class CommunityFloorsController extends BaseController
{
    @Autowired
    private ICommunityFloorsService communityFloorsService;

    /**
     * 查询floors列表
     */
    @PreAuthorize("@ss.hasPermi('system:floors:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommunityFloorsDto communityFloorsDto)
    {
        List<CommunityFloors> list = communityFloorsService.selectCommunityFloorsList(communityFloorsDto);
        startPage();
        return getDataTable(list);
    }

//    /**
//     * 导出floors列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:floors:export')")
//    @Log(title = "floors", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, CommunityFloors communityFloors)
//    {
//        List<CommunityFloors> list = communityFloorsService.selectCommunityFloorsList(communityFloors);
//        ExcelUtil<CommunityFloors> util = new ExcelUtil<CommunityFloors>(CommunityFloors.class);
//        util.exportExcel(response, list, "floors数据");
//    }

    /**
     * 获取floors详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:floors:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(communityFloorsService.selectCommunityFloorsById(id));
    }

    /**
     * 新增floors
     */
    @PreAuthorize("@ss.hasPermi('system:floors:add')")
    @Log(title = "floors", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommunityFloorsDto communityFloorsDto)
    {
        try{
            return toAjax(communityFloorsService.insertCommunityFloors(communityFloorsDto));
        } catch (Exception e) {
            return AjaxResult.error("该楼宇及单元下的楼层已存在");
        }
    }

    /**
     * 修改floors
     */
    @PreAuthorize("@ss.hasPermi('system:floors:edit')")
    @Log(title = "floors", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommunityFloors communityFloors)
    {
        return toAjax(communityFloorsService.updateCommunityFloors(communityFloors));
    }

    /**
     * 删除floors
     */
    @PreAuthorize("@ss.hasPermi('system:floors:remove')")
    @Log(title = "floors", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(communityFloorsService.deleteCommunityFloorsByIds(ids));
    }

    @PostMapping("listFloors")
    public TableDataInfo listFloors(){
        return getDataTable(communityFloorsService.listFloors());
    }
}
